{% spaceless %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <title>
        {% if meta.title %}
            {{ meta.title }} | 
        {% endif %}
        {{ site_title }}
    </title>
    <link rel="icon" type="image/x-icon" href="/favicon.ico">
    {% if meta.description %}
        <meta name="description" content="{{ meta.description }}"/>
        {# i don't understand what og:... is good for. i hope i'm doing this right. #}
        <meta property="og:description" content="{{ meta.description }}"/>
    {% endif %}
    {% if meta.robots %}
        <meta name="robots" content="{{ meta.robots }}">
    {% endif %}
    {# i don't understand what og:... is good for. i hope i'm doing this right. #}
    <meta property="og:type" content="{{current_page.meta.template}}"/>
    <meta property="og:title"
        content="{{ current_page.title }} | {{ site_title }}"/>
    <meta property="og:url" content="{{ current_page.url }}"/>
    <meta property="og:site_name" content="{{ site_title }}"/>
    {% if current_page %}
        <link rel="canonical" href="{{ current_page.url }}" />
    {% endif %}
    <link rel="stylesheet" href="{{ theme_url }}/css/style.css" type="text/css" />
    <link rel="stylesheet" href="{{ theme_url }}/css/colors-{{ config.tagblog.colors }}.css" type="text/css" />
    {% if meta.template == 404 %}<link rel="stylesheet" href="{{ theme_url }}/css/404.css" type="text/css" />{% endif %}
    {% if meta.template == config.TableOfContent.template or not config.TableOfContent.template %}<link rel="stylesheet" href="{{ theme_url }}/css/TableOfContent.css" type="text/css" />{% endif %}
</head>
<body>
    <header>
        <div class="inner flexbox">
            {% include 'includes/topmenu.twig' ignore missing %}
            <a href="{{ "index"|link }}"><img src="{{ config.tagblog.site_logo }}" alt="{{- site_title -}}"></a>
            <div class="title">
                <a href="{{ "index"|link }}">{{ site_title }}</a>
            </div>
        </div>
    </header>
    <div class="inner">
        <section id="sidebar">
            {# looping over all pages to pick out what we need (i.e. pages with tags) #}
            {% set tag_count = {} %} {# tag_count contains: tag : count of occurences #}
            {% set tag_pages = {} %} {# tag_pages contains: tag : page ids of occurences hash '#' separated #}
            {% set aut_count = {} %} {# aut_count contains: author : count of occurences #}
            {% set aut_pages = {} %} {# aut_pages contains: author : page ids of occurences hash '#' separated #}
            {% set tbpages = {} %} {# contains ALL pages to be considered, saves some cycles later on #}
            {% for page in pages if page.meta.tags or page.meta.author %}
                {% if page.meta.tags %}
                    {% set tbpages = tbpages|merge([page]) %}
                    {% for tag in page.meta.tags|split(',') %}
                        {# tag_count contains: tag : count of occurences #}
                        {% set tag_count = tag_count|merge({(tag):(tag_count[tag] + 1)}) %}
                        {# tag_pages contains: tag : page ids of occurences hash '#' separated #}
                        {% set tag_pages = tag_pages|merge( { (tag) : (tag_pages[tag] ~ '#' ~ page.id) } ) %}
                    {% endfor %}
                {% endif %}
                {% if page.meta.author %}
                    {% set aut_count = aut_count|merge({(page.meta.author):(aut_count[page.meta.author] + 1)}) %}
                    {% set aut_pages = aut_pages|merge({(page.meta.author):(aut_pages[page.meta.author]~'#'~page.id)}) %}
                {% endif %}
            {% endfor %}

            {# sort tag_count into tag_sorting, according to config options #}
            {% set tag_sorting = {} %}
            {% if config.tagblog.tagsort.method == 'numerical' %}
                {% set tag_sorting = tag_count|sort %}
            {% elseif config.tagblog.tagsort.method == 'alphabetical' %}
                {% for tag in tag_count|keys|sort %}
                    {% set tag_sorting = tag_sorting|merge({(tag): tag_count[tag] }) %}
                {% endfor %}
            {% endif %}
            {# ... and sort it back into tag_count array #}
            {% if config.tagblog.tagsort.reverse %}
                {% set tag_count = tag_sorting|reverse %}
            {% else %}
                {% set tag_count = tag_sorting %}
            {% endif %}

        <input id="menu-icon" type="checkbox"/>
        <label for="menu-icon">≡<span> menu</span></label>
        <nav id="menu-icon"> {# this nav's visibility is controled by the checkbox above! #}
        {% include 'includes/sidebar-top.twig' ignore missing %}
        {# Tag List #}
            {% if config.tagblog.taglist.enabled and tag_count %}
                <div class="tag_list">
                    {% if config.tagblog.taglist.title %}<h2>{{ config.tagblog.taglist.title }}</h2>{% endif %}
                    <ul>
                        {% for tag,total in tag_count %}
                            <li>
                                <a href="/tags?q={{tag}}">{{ tag }}{% if config.tagblog.tagcount.enabled %}<span> ({{ total }})</span>{% endif %}</a>
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}

        {# Tag Cloud #}
            {% if config.tagblog.tagcloud.enabled and tag_count %}

                {# Levels Validation  #}
                {% if max(tag_count) < config.tagblog.tagcloud.maxoccur %}
                    {% set tagmaxoccur = max(tag_count) %}
                {% else %}
                    {% set tagmaxoccur = config.tagblog.tagcloud.maxoccur %}
                {% endif %}

                {# Render Tag Cloud #}
                <div class="tag_cloud">
                    {% if config.tagblog.tagcloud.title %}<div>{{ config.tagblog.tagcloud.title }}</div>{% endif %}
                    <ul>
                        {% for tag,total in tag_count %}
                            {% if total > tagmaxoccur %}{% set size = tagmaxoccur %}{% else %}{% set size = total %}
                            {% endif %}
                            {% if size == 1 %}{% set fontsize = config.tagblog.tagcloud.fontsizemin %}
                                {% else %}
                                    {# calculate tag font size - https://stackoverflow.com/a/3717340 #}
                                    {% set fontsize = ( (size / tagmaxoccur) * ( config.tagblog.tagcloud.fontsizemax - config.tagblog.tagcloud.fontsizemin ) + config.tagblog.tagcloud.fontsizemin ) %}
                            {% endif %}
                            <li style="font-size:{{ fontsize }}rem;">
                            <a href="/tags?q={{tag}}">{{ tag }}{% if config.tagblog.tagcount.enabled %}<span>{{ total }}</span>{% endif %}</a></li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
            <hr />
            <div class="clearfix">
                {% if config.tagblog.search.enabled %}
                {# searches go to the /search.md page #}
                <form class="search paragraph" id="search_form" action="/search">
                    {# <label for="search_input">Search» </label> #}
                    <span><input title='The search function is primitive. It only searches for the literal string as entered.
» fluffy cloud
will search only the exact string
» "fluffy cloud".
Searches are case insensitive. Tags and some special characters are stripped.' class="submit" type="submit" value="Search" /></span>
                    <span><input pattern=".{0{{config.tagblog.search.minlength}},}" required title="{{config.tagblog.search.minlength}} characters minimum" size="15" type="text" id="search_input" name="q" /></span>
                </form>
                {% endif %}
                {% if config.tagblog.feed.enabled %}
                <div class="rss paragraph"><a href="/feed">RSS Feed</a></div>
                {% endif %}
                {% include 'includes/sidebar-bottom.twig' ignore missing %}
            </div>
        </nav>

        </section>
        <section id="content">

{% endspaceless %}
            {% block content %}
                {% set length = tbpages|length %}
                <article><h3>All Articles ({{length}})</h3></article>
                {% if config.tagblog.paging > 0 and length > config.tagblog.paging %}
                {# Paginate over all pages with tags #}
                    {% set p = url_param('p','int',1) %}
                    {% set offset = (p - 1) * config.tagblog.paging %}
                    {% set lastpage = length//config.tagblog.paging %}
                    {% if length%config.tagblog.paging > 0 %}{% set lastpage = lastpage + 1 %}{% endif %}
                    {% for page in tbpages|slice(offset,config.tagblog.paging) %}
                        {% include 'includes/postlistitem.twig' %}
                    {% endfor %}
                    <div class="pagination clearfix">
                        {% if offset >= config.tagblog.paging %}
                            <div class="previous"><a href="{{current_page.url}}?p={{ p - 1 }}">&lt;Previous Page</a></div>
                        {% endif %}
                        {# {% if offset >= config.tagblog.paging and offset + config.tagblog.paging < length %} | {% endif %} #}
                        <div class="current">- {{ p }}/{{ lastpage }} -</div>
                        {% if offset + config.tagblog.paging < length %}
                            <div class="next"><a href="{{current_page.url}}?p={{ p + 1 }}">Next Page&gt;</a></div>
                        {% endif %}
                    </div>
                {% else %}
                {# just list all pages #}
                    {% for page in tbpages %}
                        {% include 'includes/postlistitem.twig' %}
                    {% endfor %}
                {% endif %}
            {% endblock %}
{% spaceless %}

        </section>
    </div>
    <footer>
        {% include 'includes/footer.twig' ignore missing %}
    </footer>
</body>
</html>
{% endspaceless %}
